/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.service.self;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.momarketing.entity.OnlineSignupConfig;
import com.boxin.ims.modules.momarketing.entity.self.OnlineSignupConfigDetail;
import com.boxin.ims.modules.momarketing.entity.self.OnlineSignupDetail;
import com.boxin.ims.modules.momarketing.dao.OnlineSignupDao;
import com.boxin.ims.modules.momarketing.dao.self.OnlineSignupConfigDetailDao;
import com.boxin.ims.modules.wechat.utils.AssertUtils;

/**
 * 在线报名自定义添加Service
 * 
 * @author sogyan
 * @version 2014-03-21
 */
@Component
@Transactional(readOnly = true)
public class OnlineSignupConfigDetailService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory
			.getLogger(OnlineSignupConfigDetailService.class);

	@Autowired
	private OnlineSignupConfigDetailDao onlineSignupConfigDetailDao;

	public OnlineSignupConfigDetail get(Long id) {
		return onlineSignupConfigDetailDao.findOne(id);
	}

	public Page<OnlineSignupConfigDetail> find(
			Page<OnlineSignupConfigDetail> page,
			OnlineSignupConfigDetail onlineSignupConfigDetail) {
		DetachedCriteria dc = onlineSignupConfigDetailDao
				.createDetachedCriteria();
		if (onlineSignupConfigDetail.getUser() != null
				&& onlineSignupConfigDetail.getUser().getId() > 0) {
			dc.add(Restrictions.eq("user.id", onlineSignupConfigDetail
					.getUser().getId()));
		}
		if(onlineSignupConfigDetail.getSignupConfigId()!=null){
			dc.add(Restrictions.eq("signupConfigId", onlineSignupConfigDetail.getSignupConfigId()));
		}
		if (StringUtils.isNotEmpty(onlineSignupConfigDetail.getName())) {
			dc.add(Restrictions.like("name",
					"%" + onlineSignupConfigDetail.getName() + "%"));
		}
		dc.addOrder(Order.desc("id"));

		Page<OnlineSignupConfigDetail> onlinePage = onlineSignupConfigDetailDao
				.find(page, dc);

		if (AssertUtils.notNull(onlinePage)) {
			List<OnlineSignupConfigDetail> onlineList = onlinePage.getList();

			OnlineSignupConfigDetail online = new OnlineSignupConfigDetail();
			List list = new ArrayList();
			for (int i = 0; i < onlineList.size(); i++) {
				list = new ArrayList();
				online = onlineList.get(i);
				String[] strings = online.getDetail().split("#");
				for (int j = 0; j < strings.length; j++) {
					list.add(strings[j]);
				}
				online.setTypes(list);

			}
		}

		if (AssertUtils.notNull(onlinePage)) {
			List<OnlineSignupConfigDetail> onlineList = onlinePage.getList();

			OnlineSignupConfigDetail online = new OnlineSignupConfigDetail();
			List list = new ArrayList();
			for (int i = 0; i < onlineList.size(); i++) {

				online = onlineList.get(i);

				for (int j = 0; j < online.getTypes().size(); j++) {

				}

			}
		}

		return onlinePage;
	}

	@Transactional(readOnly = false)
	public void save(OnlineSignupConfigDetail onlineSignupConfigDetail) {
		onlineSignupConfigDetailDao.save(onlineSignupConfigDetail);
	}

	@Transactional(readOnly = false)
	public void delete(Long id) {
		onlineSignupConfigDetailDao.deleteById(id);
	}
	
	public List<OnlineSignupConfigDetail> findList(OnlineSignupConfig onlineSignupConfig, User user){
		DetachedCriteria dc = onlineSignupConfigDetailDao.createDetachedCriteria();
		if (onlineSignupConfig!=null && onlineSignupConfig.getId()!=null) {
			dc.add(Restrictions.eq("signupConfigId", onlineSignupConfig.getId()));
		}
		if (user!=null && user.getId()!=null) {
			dc.add(Restrictions.eq("user.id", user.getId()));
		}
		dc.add(Restrictions.eq("flag", "1"));
		dc.addOrder(Order.desc("id"));
		List<OnlineSignupConfigDetail> configDetailList=onlineSignupConfigDetailDao.find(dc);
		return configDetailList==null?null:configDetailList;
	}
	
	

}
